package com.scpii.universal.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.scpii.universal.exception.GenericException;

public class IosDownload {

	private int appid;
	private String iosURL;
	private String iconurl;
	private String appName;

	public IosDownload(int appid,String iosURL, String iconurl,String appName) {
		super();
		this.appid = appid;
		this.iosURL = iosURL;
		this.iconurl = iconurl;
		this.appName = appName;
	}

	public void getIosDownloadUrl(String pListPath) {
		OutputFormat format = new OutputFormat("  ", true, "utf-8");
		Document listDoc = DocumentHelper.createDocument();
		listDoc.setXMLEncoding("utf-8");
		DocumentFactory documentFactory = new DocumentFactory();
		DocumentType documentType = documentFactory.createDocType("plist",
				"-//Apple//DTD PLIST 1.0//EN",
				"http://www.apple.com/DTDs/PropertyList-1.0.dtd");
		listDoc.setDocType(documentType);
		Element pB = listDoc.addElement("plist");
		pB.addAttribute("version", "1.0");
		try {
			Element di = pB.addElement("dict");
			Element key = di.addElement("key");
			key.setText("items");
			Element array = di.addElement("array");
			Element d2 = array.addElement("dict");
			Element key2 = d2.addElement("key");
			key2.setText("assets");
			Element array21 = d2.addElement("array");
			Element di3 = array21.addElement("dict");
			Element key4 = di3.addElement("key");
			key4.setText("kind");
			Element string4 = di3.addElement("string");
			string4.setText("software-package");
			Element key42 = di3.addElement("key");
			key42.setText("url");
			Element string42 = di3.addElement("string");
			string42.setText(iosURL);
			Element di32 = array21.addElement("dict");
			Element key321 = di32.addElement("key");
			key321.setText("kind");
			Element string322 = di32.addElement("string");
			string322.setText("display-image");
			Element key323 = di32.addElement("key");
			key323.setText("needs-shine");
			di32.addElement("true");
			Element key324 = di32.addElement("key");
			key324.addText("url");
			Element string325 = di32.addElement("string");
			string325.addText(iconurl);
			Element key22 = d2.addElement("key");
			key22.addText("metadata");
			Element dict23 = d2.addElement("dict");
			Element key231 = dict23.addElement("key");
			key231.addText("bundle-identifier");
			Element string232 = dict23.addElement("string");
			string232.addText("com.scpii.universal"+appid);
			Element key233 = dict23.addElement("key");
			key233.addText("bundle-version");
			Element string234 = dict23.addElement("string");
			string234.addText("1.0");
			Element key235 = dict23.addElement("key");
			key235.addText("kind");
			Element string236 = dict23.addElement("string");
			string236.addText("software");
			Element key237 = dict23.addElement("key");
			key237.addText("subtitle");
			Element string238 = dict23.addElement("string");
			string238.addText("Apple");
			Element key239 = dict23.addElement("key");
			key239.addText("title");
			Element string2310 = dict23.addElement("string");
			string2310.addText(appName);
			OutputStream fos = new FileOutputStream(pListPath);
			XMLWriter xmlWriter = new XMLWriter(fos, format);
			xmlWriter.write(listDoc);
			xmlWriter.close();
		} catch (IOException e) {
			throw new GenericException("写ios dlist文件错误", e);
		}

	}

}
